<?php
/**
 * Created by PhpStorm.
 * User: Lyq
 * Date: 2018/8/22
 * Time: 上午8:21
 */
namespace app\admin\controller;

use think\Exception;
use think\Db;

class Agreement extends Common
{
    //合约列表
    public function index()
    {
        $this->templateTitle('信息互通合约列表');
        $this->assign('status_list',get_bussiness_agreement_status('',2));
        //判断条件
        $where['ba.id']=['gt',0];

        //搜索条件
        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition)
        {
            $where["sender.title|sender.id"] = $condition;
        }

        $condition2 = request()->param("condition2");
        $this->assign("condition2", $condition2);
        if ($condition2)
        {
            $where["receiver.title|receiver.id"] = $condition2;
        }

        $status = request()->param('status');
        $this->assign('status',$status);
        if($status!=''){
            $where['ba.status'] = $status;
        }

        //获取商会列表
        $list = Db::name("bussiness_agreement")
            ->alias('ba')
            ->join('__BUSSINESS__ sender','sender.id=ba.sender_bus_id','LEFT')
            ->join('__BUSSINESS__ receiver','receiver.id=ba.receiver_bus_id','LEFT')
            ->where($where)
            ->order("ba.update_time DESC")
            ->field('ba.*,sender.title as sender_name,receiver.title as receiver_name')
            ->paginate(10);
        $this->assign("list", $list);

        return $this->fetch();
    }


    //
    public function status()
    {
        if (request()->isAjax())
        {

            $id = request()->param("id");

            $status = Db::name('bussiness_agreement')->where('id',$id)->value('status');
            $status = $status==0?1:0;

            Db::name("bussiness_agreement")
                ->where("id", $id)
                ->update(array(
                    "status" => $status,
                    "update_time" => now_datetime(),
                ));

            $this->ajaxSuccess('状态修改成功');
        }
    }

    //删除
    public function remove()
    {
        if (request()->isAjax())
        {

            $id = request()->param("id");

            try
            {

                $del = Db::name("bussiness_agreement")
                    ->where("id", $id)
                    ->delete();

                if($del){
                    $this->ajaxSuccess('合约删除成功');
                }else{
                    $this->ajaxError('合约删除失败');
                }
            }
            catch (Exception $exception)
            {
                $this->ajaxError($exception->getMessage());
            }
        }
    }


}